<?php
if(!defined('IN_KKFRAME')) exit();
define('IN_ADMINCP', true);
include SYSTEM_ROOT.'function/admin.php';
if ($_K['member']['adminid'] != 1) showmessage('您无权访问控制面板');
$action = array('updater');
if (in_array($_GET['action'], $action)){
	include module("admin/{$_GET[action]}");
	exit();
}
if ($_POST['hash'] == $_K['formhash']){
	$_K['config']['main']['sitename'] = dstripslashes($_POST['main_sitename']);
	$_K['config']['main']['footerjs'] = dstripslashes($_POST['main_footerjs']);
	$_K['config']['seo']['keyword'] = dstripslashes($_POST['seo_keyword']);
	$_K['config']['seo']['description'] = dstripslashes($_POST['seo_description']);
	$_K['config']['seo']['header'] = dstripslashes($_POST['header']);
	$_K['config']['others']['force_domain'] = dstripslashes($_POST['force_domain']);
	$_K['config']['style']['style_dir'] = dstripslashes($_POST['style_dir']);
	if($_POST['new_forum']){
		$fid = ++$_K['config']['others']['next_fid'];
		$_K['config']['forums'][$fid] = dstripslashes($_POST['new_forum']);
	}
	if($_POST['delete_forum'] && is_array($_POST['delete_forum'])){
		foreach($_POST['delete_forum'] as $fid){
			if(!$_K['config']['forums'][$fid] || !$fid) continue;
			DB::query("UPDATE thread SET fid=0 WHERE fid='{$fid}'");
			unset($_K['config']['forums'][$fid]);
		}
	}
	$config_data = '<?php if(defined(\'IN_KKFRAME\')) $_K[\'config\'] = '.var_export($_K['config'], true).'; ?>';
	file_put_contents(ROOT.'/data/config.php', $config_data);
	showmessage('设置已保存！', referer());
}
cpheader();
echo <<<EOF
<form method="post" id="adminform" action="admin.php">
<input type="hidden" name="hash" value="{$_K[formhash]}" />
<div class="page-headers"><h3 class="head">论坛基础设置</h3></div>
<p><label for="main_sitename">站点名称：</label></p>
<p><input type="text" name="main_sitename" id="main_sitename" value="{$_K[config][main][sitename]}"></p>
<p><label for="force_domain">强制站点域名：</label></p>
<p><input type="text" name="force_domain" id="force_domain" value="{$_K[config][others][force_domain]}" placeholder="例：bbs.kookxiang.com"> （自动跳转到正确的域名）</p>
<p><label for="main_footerjs">页脚辅助JS设置：（将插入到 body 标签末尾，可以插入统计代码）</label></p>
<p><textarea name="main_footerjs" id="main_footerjs">{$_K[config][main][footerjs]}</textarea></p>
<p><input type="submit" class="g-button g-button-submit" value="&nbsp; 保存修改 &nbsp;"></p>
<br>
<div class="page-headers"><h3 class="head">KKBBS 系统工具</h3></div>
<p><a href="admin.php?action=updater" class="g-button g-button-submit">&nbsp; 检测更新 &nbsp;</a></p>
<br>
<div class="page-headers"><h3 class="head">帖子分类设置</h3></div>
<p>分类 0：未分类 &nbsp;（默认分类，无法删除）</p>
EOF;
foreach($_K['config']['forums'] as $forumid=>$forumname){
	if(!$forumid) continue;
	echo "<p><label><input type=\"checkbox\" name=\"delete_forum[]\" value=\"{$forumid}\" /> 删</label> &nbsp; 分类 {$forumid}：{$forumname}</p>";
}
echo <<<EOF
<p>注：删除分类后该分类的帖子将自动转到“未分类”下</p>
<p><label>添加分类： &nbsp; <input type="text" name="new_forum" placeholder="分类名称" /></label></p>
<p><input type="submit" class="g-button g-button-submit" value="&nbsp; 保存修改 &nbsp;"></p>
<br>
<div class="page-headers"><h3 class="head">风格设置</h3></div>
EOF;
$handle = opendir(ROOT.'template');
while(true){
	$file = readdir($handle);
	if (!$file) break;
	if ($file=='.' || $file=='..' || !is_dir(ROOT.'template/'.$file)) continue;
	$checked = $_K['config']['style']['style_dir'] == $file ? 'checked ' : '';
	echo "<p><label><input type=\"radio\" name=\"style_dir\" value=\"{$file}\" {$checked}/> $file</label></p>";
}
echo <<<EOF
<p><input type="submit" class="g-button g-button-submit" value="&nbsp; 保存修改 &nbsp;"></p>
<br>
<div class="page-headers"><h3 class="head">SEO 设置</h3></div>
<p><label for="seo_keyword">SEO 关键字：</label></p>
<p><input type="text" name="seo_keyword" id="seo_keyword" value="{$_K[config][seo][keyword]}"> &nbsp; （多个关键字请使用英文逗号分隔）</p>
<p><label for="seo_description">SEO 站点描述：（没有帖子描述信息时自动显示本信息）</label></p>
<p><input type="text" name="seo_description" id="seo_description" value="{$_K[config][seo][description]}"></p>
<p><label for="seo_header">页面其他头部信息：（将插入到 head 标签中，多为 meta 标签）</label></p>
<p><textarea name="seo_header" id="seo_header">{$_K[config][seo][header]}</textarea></p>
<p><input type="submit" class="g-button g-button-submit" value="&nbsp; 保存修改 &nbsp;"></p>
EOF;
HOOK::run("admin_config");
echo '</form>';
cpfooter();